<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\adminService;
use think\facade\Db;
use think\facade\View;

class Role extends Common{
    public function roleList(){
        //查询权限分类数据
        $list = (new adminService())->roleList();
        $number = count($list);
        View::assign('list',$list);
        View::assign('number',$number);
        return View::fetch('admin_role');
    }
    public function roleAdd(){
        //查询权限
        $list = (new adminService())->roleAst();
        View::assign('list',$list);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //验证表单信息
            $validate = $this->validate($data,[
                'title|角色名称' => 'require|length:2,30',
                'rules|角色权限' => 'require',
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            //判断分类名称是否重复
            $info = (new adminService())->roleName($data['title']);
            if($info){
                $this->error('角色已存在');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            $rules = $data['rules'];
            $data['rules']=implode(",",$rules);
            //将数据插入数据库
            // dump($data);exit;
            $result = (new adminService())->roleAdd($data);
            //判断数据是否插入成功
            if($result){
                $this->success('添加角色成功',url('roleList'));
            }else{
                $this->error('添加角色失败',url('roleAdd'));
            }
        }else{
            return View::fetch('role_add');
        }
    }
    public function roleEdit(){
        //查询权限
        $list = (new adminService())->roleAst();
        View::assign('list',$list);
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('角色不存在');
            }
            $info = (new adminService())->roleIdList($id);
            View::assign('info',$info);
            $rules = explode(',', $info['rules']);
            View::assign('arr', $rules);
            return View::fetch('role_edit');
        }else{
            $data = request()->post();
            $id = $data['id'];
            //验证表单信息
            $validate=  $this->validate($data,[
                'title|角色名称' => 'require|length:3,30',
                'rules|角色权限' => 'require',
            ]);
            if($validate !== true){
				$this->error($validate);
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            $rules = $data['rules'];
            $data['rules']=implode(",",$rules);
            //将数据插入数据库
            // dump($data);exit;
            $result = (new adminService())->roleUpdate($data);
            //判断数据是否插入成功
            if($result){
                $this->success('修改角色成功',url('roleList'));
            }else{
                $this->error('修改角色失败',url('roleEdit',['id' => $id]));
            }
        }
    }
    public function roleDel(){
		$request = request();
		$id = $request->param('id');
		$validate = $this->validate($request->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
        $info = (new adminService())->roleIdList($id);
        if(empty($info)){
            $this->error('该角色不存在');
		}
        $result = Db::name('auth_group')->delete($id);
        if($result){
            $this->success('删除角色成功',url('roleList'));
		}else{
            $this->success('删除角色失败',url('roleList'));
		}
	}
}